<?php
  
  include_once "Proxy.php";
  
  class MusicBrainz extends Proxy {
    private static $url = 'http://musicbrainz.org/ws/1/';
    private static $url_as = 'http://ws.audioscrobbler.com/1.0/';
    private $method = 'track';
    private $artist_id = 0;
    private $track_id = 0;
    private $release_id = 0;
    private $result, $searchurl, $artist, $songtitle;
    private $lucene_search = false;
    private $limit = 10;
    //http://musicbrainz.org/ws/1/artist/?type=xml&name=
    //ttp://musicbrainz.org/ws/1/release/ID?type=xml&inc=artist+counts+release-events+discs+tracks+artist-rels+label-rels+release-rels+track-rels+url-rels+track-level-rels+labels
    public function MusicBrainz() { parent::Proxy(); }    
    
    public function search($artist=0, $songtitle=0) {
      
      if(!(isset($this->artist) && isset($this->songtitle))) {
        $this->artist = $artist;
        $this->songtitle = $songtitle;
      }
      
      switch($this->method){
        case 'track':
          if($this->lucene_search){
            $this->searchurl = self::$url.'track/?type=xml&query='.rawurlencode($this->songtitle).'~0.7%20artist:'.rawurlencode($this->artist).'~0.8&inc=artist+releases+artist-rels&limit='.$this->limit;
            break;
          }
          if($this->track_id)
            $this->searchurl =  self::$url.'track/'.$this->track_id.'?type=xml&inc=artist+releases+artist-rels+label-rels+release-rels+track-rels+url-rels';
          else
            $this->searchurl =  self::$url.'track/?type=xml&artist='.rawurlencode($this->artist).'&title='.rawurlencode($this->songtitle).'&limit=10';
        break;
        case 'artist':
          if($this->artist_id)
            $this->searchurl =  self::$url.'track/'.$this->artist_id.'?type=xml&inc=sa-Official+release-rels+track-rels+url-rels';
        break;
        case 'release_img':
           // N.B. here the $song_title rapresent the release title!!!
           $this->searchurl = self::$url_as.'album/'.rawurlencode($this->artist).'/'.rawurlencode($this->songtitle).'/info.xml';
        break;
        case 'release':          
          if($this->release_id)
           $this->searchurl =  self::$url.'release/'.$this->release_id.'?type=xml&inc=artist+counts+release-events+discs+tracks+release-rels+track-rels+url-rels';
          else
           $this->searchurl =  self::$url.'release/releasetypes=Official&artistid='.$this->artist_id.'&limit=50';       
        break;
        case 'tags':
          $this->searchurl = self::$url_as.'track/'.rawurlencode($this->artist).'/'.rawurlencode($this->songtitle).'/toptags.xml';
        break;
      }
      
      curl_setopt($this->curl, CURLOPT_URL, $this->searchurl);
      $this->result = curl_exec($this->curl);
 
      if(($status = curl_getinfo($this->curl, CURLINFO_HTTP_CODE)) != 200){
        $this->result = false;
        $errormsg = ($this->method=='release_img'||$this->method=='tags'?$this->searchurl = self::$url_as.rawurlencode($this->artist).'/'.rawurlencode($this->songtitle).'/info.xml':self::$url.$this->method.'/'.$this->release_id.'?type=xml&inc=artist+counts+releas');
        throw new MTException('Error while connecting to MusicBrainz:'.$errormsg, $status);
      }
      
      if($this->method == 'track') $this->getTrackList();
      //if($this->method == 'release_img') $this->extractReleaseImg();
      
      return $this;
    }
   public function getReleaseImg() {
   	 $xml = new SimpleXMLElement($this->result);
     if($xml->coverart != null)
     	return (string)$xml->coverart->medium;
     return false;
   }
   
   public function getTags(){
   
   	 $xml = new SimpleXMLElement($this->result);
     if(($noTags = sizeof($xml->tag)) == 0)
      return 0;
     // return at most the first 10 tags
     $noTags = $noTags > 10 ? 10 : $noTags;
     $tags = array();
     for($t=0; $t < $noTags; $t++){
     	array_push($tags, array(name=>$xml->tag[$t]->name[0], count=>$xml->tag[$t]->count[0]));
     }
     
     return $tags;
   }
   
   public function getTrackList() {
      $xml = new SimpleXMLElement($this->result);
     
      if(!sizeof($xml->{'track-list'}->track))
      {
        if($this->lucene_search){
          $this->result = '<mb res="no match">'.$this->searchurl.'</mb>';
          return;
        }
        
        $this->lucene_search = true;
        $this->search();
      }
      
    }
    
   private function getAlbum() {
      $this->xml = new SimpleXMLElement($this->result);
     
      //$this->track_id = (string) $this->xml->{'track-list'}->track[0][id];
      $this->release_id = (string) $this->xml->{'track-list'}->track[0]->{'release-list'}->release[0][id];
      $this->artist_id = (string) $this->xml->{'track-list'}->track[0]->artist[id];

      
      if(empty($this->release_id) || !$this->release_id){
        $this->release_id = -1;
        //$this->result = '<mb res="no match">'.$this->searchurl.'</mb>';
      }
      else
        $this->method = 'release';
        
      $this->search();
    }
   
   public function sendResult() {
    Proxy::send($this->result);
   }
   
   public function setTrackId($id) {
     $this->track_id = $id;
   }
   
   public function setArtistId($id) {
     $this->artist_id = $id;
   }
   
   public function setReleaseId($id) {
     $this->release_id = $id;
   }
   
   public function setMethod($method) {
     switch($method){
       case 'track':
        $this->method = 'track';
       break;
       case 'artist':
        $this->method = 'artist';
       break;
       case 'release':
        $this->method = 'release';
       break;
       case 'release_img':
        $this->method = 'release_img';
       break;
       case 'tags':
        $this->method = 'tags';
       break;
     }
   }
  }
